using System.Collections;

namespace Unbound.Unbinders
{
	internal class EnumerableUnbinder : ISpecificUnbinder
	{
		readonly IUnbinder _unbinder;

		public EnumerableUnbinder(IUnbinder unbinder)
		{
			_unbinder = unbinder;
		}

		public void Unbind(IUnbindingRequest request)
		{
			var counter = 0;
			var prefixFormatString = request.Prefix + "[{0}]";

			foreach (var element in (IEnumerable) request.Source)
			{
				var modifiedPrefix = string.Format(prefixFormatString, counter++);
				request.Result.Merge(_unbinder.Unbind(element, modifiedPrefix));
			}
		}

		public bool AppropriatelyUnbinds(IUnbindingRequest request)
		{
			return request.Source as IEnumerable != null;
		}
	}
}